﻿/************************************************
* Copyright (C) 2011 东北农业大学(NEAU) 版权所有。
* 文件名：
* 版本号：
* 文件功能描述：
* 作者：
* 创建日期:
* 版本号：
* 修改人：
* 修改日期：
* 修改描述：
************************************************/
Ext.BLANK_IMAGE_URL = '/script/ext/resources/images/s.gif';
Ext.onReady(function () {
    function GetDepmetName(depmetId) {
        var str = Jxym.ajax.request("server/User/DepmetGetModel.aspx?depmetId=" + depmetId);
        var json = Ext.util.JSON.decode(str);
        return json.Name;
    }
    Ext.QuickTips.init();
    /*proxy*/
    var proxy = new Ext.data.HttpProxy({ url: "SERVER/sites.aspx" });
    /*定义数据模型*/
    var SiteInfo = new Ext.data.Record.create([
            { name: 'ID', type: 'string' },
            { name: 'Title', type: 'string' },
            { name: 'Name', type: 'string' },
            { name: 'Banner', type: 'string' },
            { name: 'Description', type: 'string' },
            { name: 'TopInfo', type: 'string' },
            { name: 'FootInfo', type: 'string' },
            { name: 'Keyword', type: 'string' },
            { name: 'TemplateName', type: 'string' },
            { name: 'TemplateHtml', type: 'string' },
            { name: 'IsDownModel', type: 'string' },
            { name: 'UserID', type: 'int'}]);
    /*reader*/
    var reader = new Ext.data.JsonReader({ totalProperty: "Count", root: "List" }, SiteInfo);
    /*groupStore*/
    var groupStore = new Ext.data.GroupingStore({ proxy: proxy, reader: reader, sortInfo: { field: 'Name', direction: 'DESC'} });
    /*设置每页条数*/
    var pageSize = 30; var cookiePageSize = parseInt(Ext.util.Cookies.get("pageSize")); if (cookiePageSize) { pageSize = cookiePageSize; }
    /*加载数据源*/
    groupStore.load({ params: { start: 0, limit: pageSize} });
    /*创建选择列模型*/
    var sm = new Ext.grid.CheckboxSelectionModel();
    /*定义列模型*/
    var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
            sm, {
                id: 'id',
                header: '编号',
                dataIndex: 'ID',
                width: 70,
                sortable: true
            }, {
                id: 'Name',
                header: '英文名',
                dataIndex: 'Name',
                width: 70,
                sortable: true
            }, {
                id: 'Title',
                header: '专题名称',
                dataIndex: 'Title',
                width: 90,
                sortable: true
            }, {
                id: 'topInfo',
                header: 'Top信息',
                dataIndex: 'TopInfo',
                width: 80,
                hidden: true
            }, {
                id: 'Banner',
                header: 'Banner',
                dataIndex: 'Banner',
                width: 170
            }, {
                id: 'TemplateName',
                header: '模版名称',
                dataIndex: 'TemplateName',
                width: 100,
                hidden: true
            }, {
                id: 'TemplateHtml',
                header: '模版HTML',
                dataIndex: 'TemplateHtml',
                width: 100,
                hidden: true
            }, {
                id: 'UserID',
                header: '创建人编号',
                dataIndex: 'UserID',
                width: 100,
                sortable: true
            }, {
                id: 'Description',
                header: '专题描述',
                dataIndex: 'Description',
                width: 100,
                hidden: true
            }, {
                id: 'Keyword',
                header: '专题关键字',
                dataIndex: 'Keyword',
                width: 100
            }, {
                id: 'footInfo',
                header: 'Foot信息',
                dataIndex: 'FootInfo',
                width: 80,
                hidden: true
            }, {
                id: 'IsDownModel',
                header: '下部模块',
                dataIndex: 'IsDownModel',
                width: 100
            }]);

    //创建下拉框
    var comboBox = new Ext.form.ComboBox({ width: 60, typeAhead: true, triggerAction: 'all', lazyRender: true, mode: 'local', title: "每页行数", emptyText: pageSize, store: new Ext.data.ArrayStore({ id: 0, fields: ['myId', 'displayText'], data: [[10, 10], [20, 20], [30, 30], [50, 50], [100, 100]] }), valueField: 'myId', displayField: 'displayText' }); comboBox.on("select", function (combo, record, index) { Ext.util.Cookies.set("pageSize", record.id); window.location.reload(); });
    //创建表格
    var grid = new Ext.grid.GridPanel({
        title: "信息列表",
        store: groupStore,
        border: true,
        margins: '5 5 0 5',
        region: 'center',
        autoExpandColumn: "Keyword",
        loadMask: { msg: '正在努力的加载，请稍等片刻..' },
        view: new Ext.grid.GroupingView(),
        cm: cm,
        sm: sm,
        tbar: [{
            text: "信息检索",
            iconCls: "icon-search-16",
            handler: function () {
                alert("信息检索");
            }
        }, {
            iconCls: 'icon-add',
            text: '添加',
            handler: function () {
                Ext.getCmp("saveBtn").setText("确认添加");
                siteForm.findById("ename").setDisabled(false);
                siteForm.getForm().reset();
                siteForm.findByType('textfield')[0].focus(true, true);
            }
        }, {
            ref: '../editBtn',
            iconCls: 'icon-edit',
            tooltip: '编辑',
            text: '编辑',
            disabled: true,
            handler: function () {
                var selectNode = grid.getSelectionModel().getSelections();
                if (selectNode) {
                    var row = selectNode[0];
                    Ext.getCmp("saveBtn").setText("确认更新");
                    siteForm.getForm().loadRecord(row);
                    siteForm.doLayout();
                }
            }
        }, {
            ref: '../removeBtn',
            iconCls: 'icon-del',
            text: '删除',
            disabled: true,
            handler: function () {
                Ext.MessageBox.confirm("确认提示", "确认要删除所选行数据？", function (btn) {
                    if (btn == "yes") {
                        var s = grid.getSelectionModel().getSelections();
                        var delR = new Array();
                        var delRid = new Array();
                        for (var i = s.length - 1; i >= 0; i--) {
                            var r = s[i];
                            if (r.get('ID') != undefined) { delRid.push(r.get('ID')); delR.push(r); }
                        }
                        Ext.Ajax.request({
                            url: "SERVER/sitesDel.aspx",
                            method: 'POST',
                            params: { id: delRid },
                            success: function (response) {
                                var json = Ext.util.JSON.decode(response.responseText);
                                if (json.success) {
                                    for (var i = 0; i < delR.length; i++) {
                                        if (json.ids.valueOf(delR[i].get('ID'))) { groupStore.remove(delR[i]) }
                                    }
                                    Ext.Msg.alert("提示", "已删除" + json.count + "行<br/>" + json.msg);
                                } else {
                                    Ext.Msg.alert("错误", json.error);
                                }
                            }
                        });
                    }
                });
            }
        }],
        bbar: new Ext.PagingToolbar({
            store: groupStore,
            pageSize: pageSize,
            displayInfo: true,
            prependButtons: true,
            items: ['每页行数', comboBox, '-'],
            displayMsg: "本页显示第{0}条到第{1}条数据，共{2}条",
            emptyMsg: "暂无记录"
        })
    });

    var TemplateName = new Ext.form.ComboBox({
        name: "TemplateName",
        anchor: '100%',
        fieldLabel: "选择模版",
        emptyText: "请选择模版",
        listEmptyText: "*暂无模版信息",
        blankText: '请选择模版',
        hiddenName: "TemplateName",
        triggerAction: 'all',
        forceSelection: true,
        allowBlank: false, //不允许为空
        editable: false,
        blankText: '信息类型不能为空', //不允许为空给的提示
        mode: 'remote',
        valueField: 'did',
        displayField: 'dname',
        store: new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({ url: "SERVER/getTemplates.aspx" }),
            reader: new Ext.data.JsonReader({}, [{ name: 'did', type: 'string', mapping: "ID" }, { name: 'dname', type: 'string', mapping: "Name"}]),
            autoLoad: true
        })
    });

    var fileField = new Ext.form.FileUploadField({
        id: 'fileField',
        anchor: '100%',
        msgTarget: 'side',
        emptyText: '请选择文件',
        fieldLabel: 'Banner',
        hiddenName: "Banner",
        name: 'Banner',
        buttonText: '',
        hidden: false,
        buttonCfg: { iconCls: 'icon-vcard_add' }
    });

    var siteForm = new Ext.form.FormPanel({
        region: 'south',
        title: "编辑信息",
        margins: '0 5 5 5',
        collapsible: true,
        expanded: false,
        frame: true,
        split: true,
        height: 190,
        labelAlign: "right",
        labelWidth: 55,
        buttonAlign: "center",
        bodyStyle: "padding:10px 5px;",
        border: true,
        items: [new Ext.form.Hidden({ ID: 'ID', value: 0, name: 'ID' }), {
            layout: 'column',
            items: [{
                columnWidth: .25,
                layout: 'form',
                items: [{
                    id: 'ename',
                    labelStyle: 'color:red',
                    xtype: 'textfield',
                    fieldLabel: '英文名',
                    name: 'Name',
                    minLength: 1,
                    minLengthText: "长度不能小于1个字符",
                    maxLength: 40,
                    maxLengthText: "长度超过了40个字符",
                    allowBlank: false,
                    blankText: '英文名不能为空',
                    anchor: '100%'
                }]
            }, {
                columnWidth: .60,
                layout: 'form',
                items: [{
                    xtype: 'textfield',
                    fieldLabel: '专题名称',
                    labelStyle: 'color:red',
                    minLength: 1,
                    minLengthText: "长度不能小于1个字符",
                    maxLength: 40,
                    maxLengthText: "长度超过了40个字符",
                    name: 'Title',
                    allowBlank: false,
                    blankText: '信息标题不能为空',
                    anchor: '100%'
                }]
            }, {
                columnWidth: .15,
                layout: 'form',
                items: [{
                    xtype: 'checkbox',
                    fieldLabel: '下部模块',
                    name: 'IsDownModel',
                    anchor: '100%'
                }]
            }]
        }, {
            layout: 'column',
            items: [{
                columnWidth: .25,
                layout: 'form',
                items: [TemplateName]
            }, {
                columnWidth: .75,
                layout: 'form',
                items: [fileField]
            }]
        }, {
            layout: 'column',
            items: [{
                columnWidth: .5,
                layout: 'form',
                items: [{
                    xtype: 'textarea',
                    fieldLabel: '描述',
                    height: 24,
                    value: '东北农业大学,继续教育中心,知识更新培训',
                    name: 'Description',
                    anchor: '100%'
                }, {
                    xtype: 'textarea',
                    fieldLabel: 'Top信息',
                    height: 24,
                    name: 'TopInfo',
                    value: '',
                    anchor: '100%'
                }]
            }, {
                columnWidth: .5,
                layout: 'form',
                items: [{
                    xtype: 'textarea',
                    fieldLabel: '关键字',
                    name: 'Keyword',
                    height: 24,
                    value: '课件发布系统系统,2013',
                    anchor: '100%'
                }, {
                    xtype: 'textarea',
                    fieldLabel: 'Foot信息',
                    height: 24,
                    name: 'FootInfo',
                    value: 'Copyright(2008) 东北农业大学<br/>地址：中国·哈尔滨·香坊区公滨路201号',
                    anchor: '100%'
                }]
            }]
        }
        /*, {
        xtype: 'textarea',
        fieldLabel: '模版HTML',
        height: 245,
        name: 'TemplateHtml',
        value: '<div class="content_u"><div class="content_u_l"><img alt="" src="Module/CMS/resources/templates/{public:\'templateName\'}/images/content_u_r_01.jpg" />\n<a href="/{public:\'siteName\'}/menu(替换课程学习的链接).html">\n<img alt="" src="Module/CMS/resources/templates/{public:\'templateName\'}/images/content_u_r_02.jpg" style="border: 0px" /></a></div><div class="content_u_c">{MyHeight:216,MyImg:"/UpFiles/banner.jpg"}</div><div class="content_u_r"><img alt="" src="Module/CMS/resources/templates/{public:\'templateName\'}/images/content_u_r_01.jpg" />\n<a href="/{public:\'siteName\'}/menu(替换课程学习的链接).html">\n<img alt="" src="Module/CMS/resources/templates/{public:\'templateName\'}/images/content_u_r_02.jpg" /></a></div><div class="clear"></div></div><div class="content_m">{MyHeight:110,MyImg:"/UpFiles/content_m.jpg"}</div>',
        anchor: '100%'
        }*/
        ],
        fileUpload: true,
        buttons: [{
            id: "saveBtn",
            text: "确认添加",
            handler: function () {
                if (siteForm.getForm().isValid()) {
                    siteForm.getForm().submit({
                        params: { siteid: siteForm.findById("ename").getValue() },
                        url: 'server/siteAdd.aspx',
                        waitTitle: '正在处理数据，请稍后...',
                        success: function (form, action) {
                            Ext.Msg.alert("请求成功", action.result.msg, function () {
                                groupStore.reload();
                            });
                        }
                    });
                }
            }
        }, {
            text: "重置",
            handler: function () {
                siteForm.getForm().reset();
            }
        }]
    });
    /*创建页面布局*/
    var viewport = new Ext.Viewport({ layout: 'border', items: [grid, siteForm] }); viewport.doLayout();
    setTimeout(function () { Ext.get('loading').remove(); Ext.get('loading-mask').fadeOut({ remove: true }); }, 400);
    //有选择行启用删除按钮
    grid.getSelectionModel().on('selectionchange', function (sm) {
        grid.removeBtn.setDisabled(sm.getCount() < 1);
        grid.editBtn.setDisabled(sm.getCount() != 1);
    });
    grid.on('rowdblclick', function (g, index, e) {
        Ext.getCmp("saveBtn").setText("确认更新");
        siteForm.findById("ename").setDisabled(true);
        var r = g.getSelectionModel().getSelected();
        siteForm.getForm().loadRecord(r);
        siteForm.doLayout();
    });
});